package org.example.controller;

import org.example.entity.User;
import org.example.service.EmailService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2025-03-07 10:00:26
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private EmailService emailService;

//    @PostMapping("/register")
//    public String registerUser() {
//        // 异步发送邮件，不阻塞 HTTP 响应
//        emailService.sendEmail("502914391@qq.com", "Welcome!");
//        return "Registration success!";
//    }
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById/{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    public ResponseEntity<Integer> add(@RequestBody User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public ResponseEntity<Boolean> edit(@RequestBody User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("del/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }


    @GetMapping("queryAllUser")
    public ResponseEntity<List<User>> queryAllUser() {
        return ResponseEntity.ok(this.userService.queryAllUsers());
    }

}

